'use strict';

const db = uniCloud.database()
const bannersCollection = db.collection('banners')

exports.main = async (event, context) => {
	try {
		// 检查集合是否存在
		const collections = await db.listCollections()
		const exists = collections.data.some(col => col.name === 'banners')
		
		if (!exists) {
			// 创建集合
			await db.createCollection('banners')
			console.log('创建banners集合成功')
		}
		
		// 创建索引
		await bannersCollection.createIndex({
			title: 'text',
			createTime: -1
		})
		
		return {
			code: 0,
			msg: '初始化成功',
			data: null
		}
	} catch (error) {
		console.error('初始化轮播图数据库失败', error)
		return {
			code: -1,
			msg: '初始化失败',
			data: null
		}
	}
} 